#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>

#include "../common/logger.h"

/*
What possible outcomes might there be if a thread executes the following code:
pthread_join(pthread_self(), NULL);
Write a program to see what actually happens on Linux. If we have a variable, tid,
containing a thread ID, how can a thread prevent itself from making a call,
pthread_join(tid, NULL), that is equivalent to the above statement?
 */

/*
ans. undefined result or Deadlock error
 */

pthread_t thread;

static void *thread_func(void *arg)
{
        printf("thread arg: %d\n", (int *)arg);
}

int main(void)
{
        pthread_create(&thread, NULL, thread_func, (void *)1);
        int ret = pthread_join(pthread_self(), NULL);
        printf("pthread_join failed: %s\n", strerror(ret));
// pthread_join failed: Resource deadlock avoided

        return EXIT_SUCCESS;
}
